package com.example.memo.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import com.example.memo.entity.NoteInfo;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class NoteHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "Note.db";
    private static final String TABLE_NAME = "Note";
    private static final String CREATE_TABLE = "create table " + TABLE_NAME +
            "(noteid integer primary key autoincrement,account text,title text,content text," +
            "date text,istop integer);";

    public NoteHelper(Context context){
        super(context,DB_NAME,null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    }

    public long createNote(String account,String title,String content){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("account",account);
        contentValues.put("title",title);
        contentValues.put("content",content);
        contentValues.put("date",getCurrentDate());
        contentValues.put("istop",0);
        return db.insert(TABLE_NAME,null,contentValues);
    }

    //    查询备忘录
    @SuppressLint("Range")
    public List<NoteInfo> queryNoteList(String account){
        SQLiteDatabase db = getReadableDatabase();
        List<NoteInfo> noteInfoList = new ArrayList<>();
        Cursor cursor = db.query(TABLE_NAME,null,"account like ?",new String[]{account},null,null,null);
        if(cursor != null){
            while(cursor.moveToNext()){
                int noteid = cursor.getInt(cursor.getColumnIndex("noteid"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                String date = cursor.getString(cursor.getColumnIndex("date"));
                int istop = cursor.getInt(cursor.getColumnIndex("istop"));

                NoteInfo noteInfo = new NoteInfo(noteid,account,title,content,date,istop);
                noteInfoList.add(noteInfo);
            }
        }
        cursor.close();
        return noteInfoList;
    }

    public void deleteNote(int noteid){
        SQLiteDatabase db = getWritableDatabase();
        db.delete(TABLE_NAME,"noteid like ?",new String[]{noteid+""});
    }

    public void changeNote(int noteid,String title,String content){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("title",title);
        contentValues.put("content",content);
        contentValues.put("date",getCurrentDate());
        db.update(TABLE_NAME,contentValues,"noteid like ?",new String[]{noteid+""});
    }

    public void setTop(int noteid,int istop){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("istop",istop);
        db.update(TABLE_NAME,contentValues,"noteid like ?",new String[]{noteid+""});
    }

    @SuppressLint("Range")
    public List<NoteInfo> search(String content0){
        String content1 = "%" + content0 + "%";
        SQLiteDatabase db = getReadableDatabase();
        List<NoteInfo> noteInfoList = new ArrayList<>();
        Cursor cursor = db.query(TABLE_NAME,null,"content like ?",new String[]{content1},null,null,null);
        if(cursor != null){
            while(cursor.moveToNext()){
                int noteid = cursor.getInt(cursor.getColumnIndex("noteid"));
                String account = cursor.getString(cursor.getColumnIndex("account"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                String date = cursor.getString(cursor.getColumnIndex("date"));
                int istop = cursor.getInt(cursor.getColumnIndex("istop"));

                NoteInfo noteInfo = new NoteInfo(noteid,account,title,content,date,istop);
                noteInfoList.add(noteInfo);
            }
        }
        cursor.close();
        return noteInfoList;
    }

    private String getCurrentDate() {
        LocalDate localDate = LocalDate.now();
        return localDate.toString();
    }
}
